---
--- Created by root.
--- DateTime: 17-11-21 下午1:59
---
local mysql = require "resty.mysql"


local _OBJ = {}


function _OBJ.new(self,sconfig)

    local db, err = mysql:new()
    if not db then
        return 1
    end
    db:set_timeout(1000) -- 1 sec

    local ok, err, errno, sqlstate = db:connect(sconfig)

    if not ok then
        return 2
    end
    db.close = close
    return db
end

function close(self)
    local sock = self.sock
    if not sock then
        return nil, "db not initialized"
    end
    if self.subscribed then
        return nil, "db subscribed state"
    end
    return sock:setkeepalive(10000, 100)
end

return _OBJ